package view.diaLogView;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.administrator.text.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import org.json.JSONObject;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import app.MyApplication;
import data.SharedPreferencesUtils;
import entity.WeAccount;
import util.BitmapUtil;
import util.GreenTreeNetworkUtil;
import util.HomeUtil;
import util.RequestDataCallback;
import util.StringUtil;
import util.Url;

/**
 * Created by Administrator on 2016/6/21.
 *图片墙的dialog
 */
public class WeAccountButtonShowDialog extends Dialog {
    private final int LOCAL = 4;//相册
    private final int CAMERA = 5;//相机
    private boolean mChoice;
    private View mView;
    private TextView mCancel;
    public static final String IMAGE_FILE_NAME= "Photo_wall.jpg";
    public  Context mContext;
    private int mPosition;
    private String Murl;
    private String mPic;
    private UploadDialog mUploadDialog;
    private String mFWQString;
    private List<WeAccount.ImgurlBean> mList;
    private WeAccountButtonShowDialog mChangHeadDialog;
    private ImageLoader mImageLoader;
    private DisplayImageOptions mDisplayImageOptions;
    private WeAccountListenerInterface mWeAccountListenerInterface;
    private File mFil;
    public final static String ALBUM_PATH = Environment.getExternalStorageDirectory() + "/download_test/";
    public WeAccountButtonShowDialog(Context context,String fWQString,List<WeAccount.ImgurlBean> list,boolean choice,int position) {
        super(context, R.style.lable_del_dialog);
        mChangHeadDialog = this;
        this.mChoice = choice;
        this.mList = list;
        this.mContext = context;
        this.mPosition = position;
        this.mFWQString = fWQString;
        mImageLoader = MyApplication.getApp().getImageLoader();
    }

    @Override public void show() {
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.share_style);
        super.show();
    }

    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params.width = mContext.getResources().getDisplayMetrics().widthPixels;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if(mChoice == true){

            mView = inflater.inflate(R.layout.weaccoun_button_showtrue_dialo, null);
            TextView album = (TextView)mView.findViewById(R.id.tv_dialog_album);//保存
            mCancel= (TextView)mView.findViewById(R.id.tv_dialog_cancel);//取消
            LinearLayout linearLayout = (LinearLayout)mView.findViewById(R.id.linear_dialog_system);
            //点击事件
            initClick(album, mCancel,linearLayout);

        }else if(mChoice == false){

            mView = inflater.inflate(R.layout.weaccoun_button_show_dialo, null);
            TextView album = (TextView)mView.findViewById(R.id.tv_dialog_album);//保存
            mCancel= (TextView)mView.findViewById(R.id.tv_dialog_cancel);//取消
            TextView photograph = (TextView)mView.findViewById(R.id.tv_dialog_photograph);//删除
            LinearLayout linearLayout = (LinearLayout) mView.findViewById(R.id.linear_dialog_local);
            //点击事件
            initClick(album, mCancel, linearLayout);
            Murl = mFWQString;
            photograph.setOnClickListener(new View.OnClickListener() {
                @Override public void onClick(View v) {
                    mChangHeadDialog.dismiss();
                    mUploadDialog = new UploadDialog(mContext,"删除中");
                    mUploadDialog.show();
                    String token = (String) SharedPreferencesUtils.getParam(mContext, HomeUtil.mTokenDB, "");
                    String id = (String)SharedPreferencesUtils.getParam(mContext,HomeUtil.mUseridDB,"");
                    HashMap map = new HashMap();
                    map.put("imgid",mList.get(mPosition).getId()); map.put("token",token); map.put("userid",id);
                    String stringJson = StringUtil.getStringUtilNew().hashMapToJson(map); //参数包
                    long time = StringUtil.getStringUtilNew().getTimeCurrent();
                    String stirngSign = StringUtil.getStringUtilNew().getSign(Url.mStirngMycenter_deleteUserImg,stringJson,token,id,time);
                    Map map1 = StringUtil.getStringUtilNew().getMapNew(mContext,stringJson,token,id,Url.mStirngMycenter_deleteUserImg,stirngSign,time);
                    GreenTreeNetworkUtil.getInstance().doPost(mContext,Murl,1,map1, new RequestDataCallback() {
                        @Override public void onSuccess(String response) {

                            try {
                                JSONObject jsonObject = new JSONObject(response);
                                String string = jsonObject.getString("code");
                                mUploadDialog.dismiss();
                                if (string.equals("1")) {
                                   Toast.makeText(mContext, "删除成功", Toast.LENGTH_SHORT).show();
                                    mWeAccountListenerInterface.doConfirm("ok");
                                }else if(string.equals("-2")){
                                    CrowdRetunDialog crowdRetunDialog = new CrowdRetunDialog(mContext);
                                    crowdRetunDialog.show();

                                }else {Toast.makeText(mContext, "删除失败", Toast.LENGTH_SHORT).show();}
                            } catch (Exception e) {e.printStackTrace();}
                        }

                        @Override public void onFailure(Throwable error) {
                            mUploadDialog.dismiss();
                            StringUtil.getStringUtilNew().getExceptionCode(mContext, error.toString());
                        }
                    });
                }
            });
        }
        setContentView(mView, params);
    }

    /**
     * 点击事件
     */
    private void initClick(TextView album,TextView cancel,LinearLayout linearLayout) {

        album.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v){
                mPic = mList.get(mPosition).getPic();
                mChangHeadDialog.dismiss();
                File file = new File(mPic);
                Bitmap bitmap = mImageLoader.loadImageSync(mPic,mDisplayImageOptions);
                try {
                    BitmapUtil.newBitmap().saveJPGFile(mContext,bitmap,file.getName(),ALBUM_PATH);
                   // saveJPGFile(bitmap,file.getName());
                    mFil = new File(ALBUM_PATH);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        HomeUtil.getHemeUtilNew().getDialogDismiss(cancel, mChangHeadDialog);
        HomeUtil.getHemeUtilNew().getDialogDismiss(linearLayout, mChangHeadDialog);
    }

    //接口回调，删除成功后返回
    public interface WeAccountListenerInterface {
        void doConfirm(String ok);}

    public void setWeAccountlistener(WeAccountListenerInterface weAccountListenerInterface) {
        this.mWeAccountListenerInterface = weAccountListenerInterface;

    }

}
